package com.jiamao.goplay.ui.welcome;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.gyf.immersionbar.ImmersionBar;
import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.base.BaseMVVMActivity;
import com.jiamao.goplay.config.Config;
import com.jiamao.goplay.databinding.WelcomeLayoutBinding;
import com.jiamao.goplay.ui.login.LoginPhoneFragment;
import com.jiamao.goplay.ui.login.LoginWeChatFragment;
import com.jiamao.goplay.ui.main.MainActivity;
import com.jiamao.goplay.ui.user.UserInvitationFragment;
import com.jiamao.goplay.ui.user.UserPairFragment;
import com.jiamao.goplay.view.CircleProgressbar;
import com.kongzue.dialog.interfaces.OnDialogButtonClickListener;
import com.kongzue.dialog.util.BaseDialog;
import com.kongzue.dialog.util.DialogSettings;
import com.kongzue.dialog.v3.MessageDialog;
import com.umeng.commonsdk.UMConfigure;

import org.json.JSONObject;

import me.goldze.mvvmhabit.base.AppManager;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.SPUtils;

public class WelComeActivity extends BaseMVVMActivity<WelcomeLayoutBinding,WelComeViewModel> {

    private boolean isClick = false;
    private CircleProgressbar mCircleProgressbar;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.welcome_layout;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

//    @Override
//    public WelComeViewModel initViewModel() {
//        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
//        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
//        return ViewModelProviders.of(this, factory).get(WelComeViewModel.class);
//    }

    @Override
    public void initViewObservable() {

    }

    @Override
    public void initData() {
        super.initData();

        ImmersionBar.with(this)
                .statusBarDarkFont(true)
                .autoDarkModeEnable(true, 0.2f)
                .init();
        initView();
        showLogoutDialog();
//        Observable.timer(1, TimeUnit.SECONDS, AndroidSchedulers.mainThread()).subscribe(new Consumer() {
//            @Override
//            public void accept(Object o) throws Exception {
//                viewModel.toMain();
//            }
//        });

    }

    public void initView(){
        Log.d("HTML","initView");
        mCircleProgressbar = (CircleProgressbar) binding.getRoot().findViewById(R.id.tv_time);
        if (mCircleProgressbar ==null){


            return;
        }
        mCircleProgressbar.setOutLineColor(Color.TRANSPARENT);
        mCircleProgressbar.setInCircleColor(Color.WHITE);
        mCircleProgressbar.setProgressColor(Color.YELLOW);
        mCircleProgressbar.setProgressLineWidth(3);
        mCircleProgressbar.setProgressType(CircleProgressbar.ProgressType.COUNT);
        mCircleProgressbar.setTimeMillis(1200);
        //mCircleProgressbar.reStart();

        mCircleProgressbar.setCountdownProgressListener(1,progressListener);

        mCircleProgressbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                if (!isClick){
                    isClick = true;
                  //  mCircleProgressbar.
                    KLog.d("点击按钮进入主页");
                    mCircleProgressbar.stop();
                    toMain();
                }


            }
        });

//        viewModel.addRxSubscribe(Observable.intervalRange(0, 3, 0, 1, TimeUnit.SECONDS)    //
//                   .compose(RxUtils.bindToLifecycle(viewModel.getLifecycleProvider())) // 请求与View周期同步
//                .compose(RxUtils.schedulersTransformer())  // 线程调度
//                //     .compose(RxUtils.exceptionTransformer())   // 网络错误的异常转换
//                .doOnSubscribe(new Consumer<Disposable>() {
//                    @Override
//                    public void accept(Disposable disposable) throws Exception {
//                            Log.d("HTML","doOnSubscribe");
//                        // dialog.show();
//                    }
//                })
//                 .doOnNext(new Consumer<Integer>() {
//                     @Override
//                     public void accept(Integer o) throws Exception {
//                         Log.d("HTML","doOnNext");
//                         mCircleProgressbar.setProgress(o);
//                     }
//                 })
//                .subscribe(new Consumer<Integer>() {
//                    @Override
//                    public void accept(Integer o) throws Exception {
//                        Log.d("HTML","subscribe");
//                    }
//                }));

    }

    @Override
    protected void onPause() {
        super.onPause();
     //   mCircleProgressbar.stop();
    }

    @Override
    protected void onResume() {
        super.onResume();
    //    mCircleProgressbar.start();
    }

    private CircleProgressbar.OnCountdownProgressListener progressListener = new CircleProgressbar.OnCountdownProgressListener() {
        @Override
        public void onProgress(int what, int progress)
        {
            Log.d("HTML","what:"+what+" progress:"+progress);
            if(what==1 && progress==100 && !isClick)
            {
                isClick = true;
                KLog.d("HTML","定时任务到了 进入主页");
                mCircleProgressbar.stop();
               toMain();

            }

        }
    };

    public void toMain(){

        overridePendingTransition(0, 0);
        if (SPUtils.getInstance().getBoolean(Config.IS_LOGIN)){
            Bundle bd =new Bundle();
            if(getIntent().hasExtra("data")) {
                bd.putString("data", getIntent().getStringExtra("data"));
            }
           startActivity(MainActivity.class,bd);
        }else {
             startContainerActivity(LoginWeChatFragment.class.getCanonicalName());
        }

        finish();

    }

    void showLogoutDialog(){

        if (!SPUtils.getInstance().getBoolean(Config.IS_XIEYI))
        MessageDialog.build(this)
                .setStyle(DialogSettings.STYLE.STYLE_IOS)
                .setTitle(R.string.tips_wxts)
                .setMessage(R.string.tips_wxts_txt)
                .setOkButton(R.string.dialog_ty, new OnDialogButtonClickListener() {
                    @Override
                    public boolean onClick(BaseDialog baseDialog, View v) {

                        SPUtils.getInstance().put(Config.IS_XIEYI,true);
                        UMConfigure.init(getApplicationContext(),  Config.UmengKey, "Umeng", UMConfigure.DEVICE_TYPE_PHONE, null);
                        mCircleProgressbar.start();
                        return false;
                    }

                }).setCancelButton(R.string.dialog_no, new OnDialogButtonClickListener() {
            @Override
            public boolean onClick(BaseDialog baseDialog, View v) {
                mCircleProgressbar.start();
                return false;
            }
        })
                .show();
        else {
            UMConfigure.init(getApplicationContext(),  Config.UmengKey, "Umeng", UMConfigure.DEVICE_TYPE_PHONE, null);
            mCircleProgressbar.start();
        }


    }


}
